﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KBVault.DAL.Entities;
using System.Web.Security;
using KBVault.DAL.Repository;

namespace KBVault.Web.Infrastructure
{
    public class SessionManager
    {
        public static void SetCurrentUser(User usr)
        {
            HttpContext.Current.Session[Constants.UserObjectSessionKey] = usr;
        }
        public static User GetCurrentUser()
        {
            if (HttpContext.Current.Session[Constants.UserObjectSessionKey] != null)
                return (User)HttpContext.Current.Session[Constants.UserObjectSessionKey];
            //bakalım cookie var mı
            var userId = FormsAuthentication.Decrypt(HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
            User usr = UserRepository.Get(Convert.ToInt32(userId));
            if (usr != null)
                HttpContext.Current.Session[Constants.UserObjectSessionKey] = usr;
            return usr;
        }
    }
}